package com.ruoyi.mx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mx.domain.MxGoodsCategory;
import com.ruoyi.mx.service.IMxGoodsCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品分类Controller
 *
 * @author ruoyi
 * @date 2021-10-25
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mx/goodsCategory" )
public class MxGoodsCategoryController extends BaseController {

    private final IMxGoodsCategoryService iMxGoodsCategoryService;

    /**
     * 查询商品分类列表
     */
    //@PreAuthorize("@ss.hasPermi('mx:goodsCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(MxGoodsCategory mxGoodsCategory)
    {
        startPage();
        LambdaQueryWrapper<MxGoodsCategory> lqw = new LambdaQueryWrapper<MxGoodsCategory>();
        if (StringUtils.isNotBlank(mxGoodsCategory.getEnable())){
            lqw.eq(MxGoodsCategory::getEnable ,mxGoodsCategory.getEnable());
        }
        if (StringUtils.isNotBlank(mxGoodsCategory.getParentId())){
            lqw.eq(MxGoodsCategory::getParentId ,mxGoodsCategory.getParentId());
        }
        if (StringUtils.isNotBlank(mxGoodsCategory.getName())){
            lqw.like(MxGoodsCategory::getName ,mxGoodsCategory.getName());
        }
        if (StringUtils.isNotBlank(mxGoodsCategory.getDescription())){
            lqw.eq(MxGoodsCategory::getDescription ,mxGoodsCategory.getDescription());
        }
        if (StringUtils.isNotBlank(mxGoodsCategory.getPicUrl())){
            lqw.eq(MxGoodsCategory::getPicUrl ,mxGoodsCategory.getPicUrl());
        }
        if (mxGoodsCategory.getSort() != null){
            lqw.eq(MxGoodsCategory::getSort ,mxGoodsCategory.getSort());
        }
        List<MxGoodsCategory> list = iMxGoodsCategoryService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 返回树形集合
     */
    @GetMapping("/tree")
    @Cacheable(value = "getGoodsCategoryTree",key ="'tree'",cacheManager="redisExpire")
    public R getGoodsCategoryTree(){
        LambdaQueryWrapper<MxGoodsCategory> lqw = new LambdaQueryWrapper<MxGoodsCategory>();
        List<MxGoodsCategory> list = iMxGoodsCategoryService.list(null);
        List<HashMap<String,Object>> treeList=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getParentId().equals("0")){
                HashMap<String,Object> map=new HashMap<>();
                ArrayList childTree=new ArrayList();
                for (int j = 0; j < list.size(); j++) {
                    if(list.get(j).getParentId().equals(list.get(i).getId())&&(!(list.get(j).getParentId().equals("0")))){
                        childTree.add(list.get(j));
                    }
                }
                map.put("name",list.get(i).getName());
                map.put("children",childTree);
                treeList.add(map);
            }

        }
        return R.success(treeList);
    }

    /**
     * 导出商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('mx:goodsCategory:export')" )
    @Log(title = "商品分类" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public R export(MxGoodsCategory mxGoodsCategory) {
        LambdaQueryWrapper<MxGoodsCategory> lqw = new LambdaQueryWrapper<MxGoodsCategory>(mxGoodsCategory);
        List<MxGoodsCategory> list = iMxGoodsCategoryService.list(lqw);
        ExcelUtil<MxGoodsCategory> util = new ExcelUtil<MxGoodsCategory>(MxGoodsCategory. class);
        return util.exportExcel(list, "goodsCategory" );
    }

    /**
     * 获取商品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('mx:goodsCategory:query')" )
    @GetMapping(value = "/{id}" )
    public R getInfo(@PathVariable("id" ) String id) {
        return R.success(iMxGoodsCategoryService.getById(id));
    }

    /**
     * 新增商品分类
     */
    @PreAuthorize("@ss.hasPermi('mx:goodsCategory:add')" )
    @Log(title = "商品分类" , businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody MxGoodsCategory mxGoodsCategory) {
        return toAjax(iMxGoodsCategoryService.save(mxGoodsCategory) ? 1 : 0);
    }

    /**
     * 修改商品分类
     */
    @PreAuthorize("@ss.hasPermi('mx:goodsCategory:edit')" )
    @Log(title = "商品分类" , businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody MxGoodsCategory mxGoodsCategory) {
        return toAjax(iMxGoodsCategoryService.updateById(mxGoodsCategory) ? 1 : 0);
    }

    /**
     * 删除商品分类
     */
    @PreAuthorize("@ss.hasPermi('mx:goodsCategory:remove')" )
    @Log(title = "商品分类" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public R remove(@PathVariable String[] ids) {
        return toAjax(iMxGoodsCategoryService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
